package excel;

import com.alibaba.fastjson.JSON;
import excel.poi.Excel;
import lombok.Data;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author CHL
 * @ClassName: MathematicsTopicDto
 * @Description: 数学算式类
 * @date 2023/12/15 16:26
 * @Version 1.0
 */
@Data
public class MathematicsTopicDto {

    @Excel(name = "日期：",  height = 35, width = 40)
    private String tppic1;
    @Excel(name = "耗时：",  height = 35, width = 40)
    private String tppic2;
    @Excel(name = "日期：",  height = 35, width = 40)
    private String tppic3;
    @Excel(name = "耗时：",  height = 35, width = 40)
    private String tppic4;
    @Excel(name = "日期：",  height = 35, width = 40)
    private String tppic5;
    @Excel(name = "耗时：",  height = 35, width = 40)
    private String tppic6;
    @Excel(name = "日期：",  height = 35, width = 40)
    private String tppic7;
    @Excel(name = "耗时：",  height = 35, width = 40)
    private String tppic8;


    public static void main(String[] args) {
//        List<MathematicsTopicDto> list = new ArrayList<>();
//        Random random = new Random();
//        int num3 = random.nextInt(2)+1;
        for (int i = 0; i <40; i++) {
            System.out.println(getMultiply(5));

        }


//        getEquation(list, "sus", 50);
//        for (MathematicsTopicDto mathematicsTopicDto : list) {
//            System.out.println(mathematicsTopicDto.getTppic1() + " " +
//                    mathematicsTopicDto.getTppic2() + " " +
//                    mathematicsTopicDto.getTppic3() + " " +
//                    mathematicsTopicDto.getTppic4() + " " +
//                    mathematicsTopicDto.getTppic5() + " ");
//        }

//        List<Integer> objects = Arrays.asList(1, 2, 3,7,4,8,9,20,19,18,17);
//        List<Integer> collect = objects.stream().sorted (Integer::compareTo).collect(Collectors.toList());

//        for (int i = 1; i < objects.size(); i++) {
//            for (int y = objects.size()-1; y > i; --y) {
//                if (objects.get(i) > objects.get(y)) {
//                    Integer temp = objects.get(i);
//                    objects.set(i, objects.get(y));
//                    objects.set(y, temp);
//                }
//            }
//
//        }


    }


    /**
     * @Description 获取算式
     * @param list    返回集合
     * @param str     生成算式类型
     * @param number  生成某数以内的算式
     * @param lineNum 生成多少行算式
     * @return response response
     * @Author chl
     * @Date 16:45 2024/7/4
     **/
    public static void getEquation(List<MathematicsTopicDto> list, String str, int number, int lineNum) {
        int a = 0;
        // 循环生成20行
        while (a < lineNum) {
            MathematicsTopicDto req = new MathematicsTopicDto();
            if (str.equals("加法")) {
                req.setTppic1(getAdd(number));
                req.setTppic2(getAdd(number));
                req.setTppic3(getAdd(number));
                req.setTppic4(getAdd(number));
                req.setTppic5(getAdd(number));
                req.setTppic6(getAdd(number));
                req.setTppic7(getAdd(number));
                req.setTppic8(getAdd(number));
            } else if (str.equals("减法")) {
                req.setTppic1(getSub(number));
                req.setTppic2(getSub(number));
                req.setTppic3(getSub(number));
                req.setTppic4(getSub(number));
                req.setTppic5(getSub(number));
                req.setTppic6(getSub(number));
                req.setTppic7(getSub(number));
                req.setTppic8(getSub(number));
            } else if (str.equals("乘法")) {
                req.setTppic1(getMultiply(number));
                req.setTppic2(getMultiply(number));
                req.setTppic3(getMultiply(number));
                req.setTppic4(getMultiply(number));
                req.setTppic5(getMultiply(number));
                req.setTppic6(getMultiply(number));
                req.setTppic7(getMultiply(number));
                req.setTppic8(getMultiply(number));
            } else if (str.equals("除法")) {
               // 除法
                req.setTppic1(getDivision(number));
                req.setTppic2(getDivision(number));
                req.setTppic3(getDivision(number));
                req.setTppic4(getDivision(number));
                req.setTppic5(getDivision(number));
                req.setTppic6(getDivision(number));
                req.setTppic7(getDivision(number));
                req.setTppic8(getDivision(number));
            } else if (str.equals("加减法")) {
                req.setTppic1(getAddAndSub(number));
                req.setTppic2(getAddAndSub(number));
                req.setTppic3(getAddAndSub(number));
                req.setTppic4(getAddAndSub(number));
                req.setTppic5(getAddAndSub(number));
                req.setTppic6(getAddAndSub(number));
                req.setTppic7(getAddAndSub(number));
                req.setTppic8(getAddAndSub(number));
            }else if (str.equals("乘除法")) {
                req.setTppic1(getMultiplyAndDivision(number));
                req.setTppic2(getMultiplyAndDivision(number));
                req.setTppic3(getMultiplyAndDivision(number));
                req.setTppic4(getMultiplyAndDivision(number));
                req.setTppic5(getMultiplyAndDivision(number));
                req.setTppic6(getMultiplyAndDivision(number));
                req.setTppic7(getMultiplyAndDivision(number));
                req.setTppic8(getMultiplyAndDivision(number));
            } else {
                // 加减乘除混合
                req.setTppic1(getMultiplyAndDivisionAndADDAndSub(number));
                req.setTppic2(getMultiplyAndDivisionAndADDAndSub(number));
                req.setTppic3(getMultiplyAndDivisionAndADDAndSub(number));
                req.setTppic4(getMultiplyAndDivisionAndADDAndSub(number));
                req.setTppic5(getMultiplyAndDivisionAndADDAndSub(number));
                req.setTppic6(getMultiplyAndDivisionAndADDAndSub(number));
                req.setTppic7(getMultiplyAndDivisionAndADDAndSub(number));
                req.setTppic8(getMultiplyAndDivisionAndADDAndSub(number));
            }

            list.add(req);
            a++;
        }

    }
    /**
     * @Author chl
     * @Description 除法算式生成
     * @Date 15:29 2024/7/4
     * @param number 某数以内的除法算式
     * @return response response
     **/
    private static String getDivision(int number) {
        Random random = new Random();
        int num = random.nextInt(number) + 1;
        int num2 = random.nextInt(number) + 1;
        int result = num * num2;
        String str1 = String.valueOf(num);

        // 统一样式
        while (str1.length() < 5) {
            str1 = str1 + " ";
        }
        String str2 = String.valueOf(num2);
        while (str2.length() < 5) {
            str2 = str2 + " ";
        }
        String resultStr = String.valueOf(result);
        while (resultStr.length() < 5) {
            resultStr = resultStr + " ";
        }

        String string = "";
        // 随机替换[1,3)之间
        int num3 = random.nextInt(3)+1;
        // 替换括号
        if (num3 == 1) {
            string = resultStr + "÷    " + str2 + " =   (     )";
        } if (num3 == 2) {
            string =  resultStr + "÷ (     )  =      " + str1;
        } if (num3 == 3) {
            string = resultStr + "÷    " + str2 + " =   (     )";
        }
        return string;
    }

    /**
     * @Author chl
     * @Description 乘法算式生成
     * @Date 15:29 2024/7/4
     * @param number 某数以内的乘法算式
     * @return response response
     **/
    private static String getMultiply(int number) {
        Random random = new Random();
        int num = random.nextInt(number) + 1;
        int num2 = random.nextInt(number) + 1;
        int result = num * num2;
        String str1 = String.valueOf(num);

        // 统一样式
        while (str1.length() < 5) {
            str1 = str1 + " ";
        }
        String str2 = String.valueOf(num2);
        while (str2.length() < 5) {
            str2 = str2 + " ";
        }

        String string = "";
        // 随机替换[1,3)之间
        int num3 = random.nextInt(3)+1;
        // 替换括号
        if (num3 == 1) {
            string = "(     )  ×    " + str2 + " =    " + result;
        } if (num3 == 2) {
            string = "   " + str1 + " × (     ) " + " =    " + result;
        } if (num3 == 3) {
            string = "   " + str1 + " ×    " + str2 + " = (     )";
        }
        return string;
    }

    private static String getAdd(int number) {
        String str = "";
        int cishu = 3; // 生成次数
        int num = number; // 保证下次比上次要小
        for (int i = 0; i < cishu; i++) {
            Random random2 = new Random();
            int suiji = 0;
            if (num == 0) {
                suiji = random2.nextInt(num + 1);
            } else {
                suiji = random2.nextInt(num);
            }
            str += suiji;
            if ((cishu - i) == 1) {
                str += "=";
            } else {
                str += "+";
            }
        }
        return str;
    }

    private static String getSub(int number) {
        int length = String.valueOf(number).length();
        String str = "%s - ";
        int cishu = 3; // 生成次数
        int num = number;
        ArrayList<Integer> objects = new ArrayList<>();
        for (int i = 0; i < cishu; i++) {
            Random random2 = new Random();
            int suiji = 0;
            String string = "";
            if ((cishu - i) == 1) {
                // 最后一次生成 第一位 [10, number+10]
                Integer collect = objects.stream().collect(Collectors.summingInt(Integer::byteValue));
                suiji = random2.nextInt(num) + collect;
                string = String.valueOf(suiji);
                switch (length - (string.length())) {
                    case 1:
                        string = "0" + string;
                        break;

                    case 2:
                        string = "00" + string;
                }
                str = String.format(str, string) + "=   ";
            } else {
                suiji = random2.nextInt(num);
                string = String.valueOf(suiji);
                objects.add(suiji);
                switch (length - (string.length())) {
                    case 1:
                        string = " " + string;
                        break;

                    case 2:
                        string = "  " + string;
                }
                str += string;
            }

            if ((cishu - i) > 2) {
                str += " - ";
            }
        }
        return str;

    }

    // 加减乘除混合
    private static String getMultiplyAndDivisionAndADDAndSub(int number) {




        return "";

    }


    // 乘除
    private static String getMultiplyAndDivision(int number) {
        Random random2 = new Random();
        // 第一位数
        int num1 = random2.nextInt(number)+1;
        int num2 = random2.nextInt(number)+1;
        int num3 = random2.nextInt(number)+1;
        int multiply = num1 * num2;
        String oper = "×➗";
        for (int i = 0; i < oper.length(); i++) {
            boolean b = true;
            while (b) {
                if (num3 == 0) {
                     num3 = random2.nextInt(number);
                }else {
                    if (multiply % num3 > 0) {
                        num3 = random2.nextInt(number);
                        continue;
                    }
                    b = false;
                }
            }

        }
       return num1 + " × " + num2 + " ➗ " + num3 + " =";
//        String operator = oper.charAt(random2.nextInt(oper.length())) + "";
//        if (operator.equals("×")) {
//            boolean b = true;
//            while (b) {
//                if (num3)
//            }
//        }else {
//
//        }


    }

    private final static String OPERATOR = "➕➖×➗";
    // 加 减
    private static String getAddAndSub(int number) {
        String str = "";

        Random random2 = new Random();
        int num1 = random2.nextInt(number);

        int num2 = random2.nextInt(number);


        int num3 = random2.nextInt(number);
        while ((num1 + num2 - num3 < 0)) {
            num3 =  random2.nextInt(number);
        }

        str = num1 + " + " + num2 +  " - " + num3 + " = ";

        // 生成的加减数
        int cishu = 3; // 生成次数
   /*     int num = 10;
        for (int i = 0; i < cishu; i++) {
            switch (i) {
                case 0:
                    str += random2.nextInt(number) + 10 + " ";
                    break;
                case 1:
                    String suiji = RandomStringUtils.random(1, "+");
                    str += suiji + " " + random2.nextInt(num) + " ";
                    break;
                case 2:
                    String suiji2 = RandomStringUtils.random(1, "-");
                    str += suiji2 + " " + random2.nextInt(num) + " = ";
                    break;

            }
        }*/


        return str;

    }


    private static int suiji(int number) {
        if (number == 0) {
            number += 1;
        }

        Random random2 = new Random();
        int random1 = random2.nextInt(number) + 1;
        return random1;
    }

    /*
     * 1.魏晋风华打卡式阅读
     * 2.python未
     * 3.老王视频未
     * 4.其他书籍总结梳理
     *
     *
     * 1.学习
     *       记忆训练
     *       技能提升
     * 2.锻炼跳绳
     *
     * 3.家人
     *
     *
     *
     *
     * */
}
